package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.mapper.ClazzsMapper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazzs;
import com.itheima.pojo.PageBody;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClazzsMapper clazzsMapper;
    @Override
    public PageBody findStudentByPage(String name, Integer degree, Integer clazzId, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Student> list = studentMapper.findStudentByPage(name, degree, clazzId);
        PageInfo<Student> pageInfo = new PageInfo<>(list);
        return new PageBody(pageInfo.getTotal(),pageInfo.getList().toArray());
    }

    @Override
    public void saveStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        student.setViolationScore(0);
        student.setViolationCount(0);
        Integer clazzId = student.getClazzId();
        Clazzs clazzsById = clazzsMapper.findClazzsById(clazzId);
        student.setClazzName(clazzsById.getName());
        studentMapper.saveStudent(student);
    }

    @Override
    public Student findStudentById(Integer id) {
        Student studentById = studentMapper.findStudentById(id);
        return studentById;
    }

    @Override
    public void updateStudentById(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        Clazzs clazzsById = clazzsMapper.findClazzsById(student.getClazzId());
        student.setClazzName(clazzsById.getName());
        studentMapper.updateStudentById(student);
    }

    @Override
    public void deleteStudentByIds(List<Integer> ids) {
        studentMapper.deleteByIds(ids);
    }

    @Override
    public void updateStudentViolation(Integer id, Integer score) {
        studentMapper.studentViolation(id,score);
    }
}
